package com.example.myapplication;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.zxing.client.android.MNScanManager;
import com.google.zxing.client.android.other.MNScanCallback;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    //new
    private AppCompatTextView mainTextView;
//old
//    private RecyclerView mainRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xxxx);
        mainTextView = findViewById(R.id.mainTextView);

        mainTextView.setOnClickListener(view -> {
            //定义广播并发送
            Intent intent = new Intent();
            intent.setPackage(getPackageName());
            intent.setAction("fangqie");
            intent.putExtra("info", "您好");
            sendBroadcast(intent);
        });

        BroadcastReceiver mTimeUpdateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent == null) return;
                String action = intent.getAction();
                if (action == null || action.isEmpty()) return;
                if (action.equals(Intent.ACTION_TIME_TICK)) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String sim = dateFormat.format(new Date());
                    int timeYear = Integer.parseInt(sim.substring(0, 4));
                    int timeMonth = Integer.parseInt(sim.substring(4, 6));
                    int timeDay = Integer.parseInt(sim.substring(6, 8));
                    int timeHour = Integer.parseInt(sim.substring(8, 10));
                    int timeMinute = Integer.parseInt(sim.substring(10, 12));
                    Log.i("时间变化广播接收", timeYear + "年" + timeMonth + "月" + timeDay + "日" + timeHour + "时" + timeMinute + "分");
                } else if (action.equals(Intent.ACTION_TIME_CHANGED)) {
                    Log.i("时间变化广播接收", "时间变化广播接收");
                }
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        registerReceiver(mTimeUpdateReceiver, filter);



        /*
        //new
        mainTextView =findViewById(R.id.mainTextView);
        mainTextView.setOnClickListener(view -> {
            //跳转到WebViewActivity并加载网址：https://gitee.com/
            Intent intent = new Intent(MainActivity.this, WebViewMainActivity.class);
            intent.putExtra("url", "https://gitee.com/");
            startActivityForResult(intent, 999);
        });
         */


        //旧2

//
//
//            MNScanManager.startScan(this, new MNScanCallback() {
//                @Override
//                public void onActivityResult(int resultCode, Intent data) {
//                    switch (resultCode) {
//                        case MNScanManager.RESULT_SUCCESS:
//                            //mainTextView.setText(resultSuccess);
//                            String resultSuccess = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_SUCCESS);
//                            Intent intent = new Intent(MainActivity.this, WebViewMainActivity.class);
//                            intent.putExtra("url", resultSuccess);
//                            startActivityForResult(intent, 999);
//                            break;
//                        case MNScanManager.RESULT_FAIL:
//                            String resultError = data.getStringExtra(MNScanManager.INTENT_KEY_RESULT_ERROR);
//                            break;
//                        case MNScanManager.RESULT_CANCLE:
//                            //showToast("取消扫码");
//                            break;
//                    }
//                }
//            });
//        });
//
//
//        Log.d("TAG", "onCreate");
//
//    }
//
//    @Override
//    protected void onStart() {
//        super.onStart();
//        Log.d("TAG", "onStart");
//    }
//    @Override
//    protected void onResume() {
//        super.onResume();
//        Log.d("TAG", "onResume");
//        List<String> list = new ArrayList<>();
//        for (String permissions : getPermissions()) {
//            if (ContextCompat.checkSelfPermission(this, permissions) != PackageManager.PERMISSION_GRANTED) {
//                list.add(permissions);
//            } else {
//                if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions)) {
//                    list.add(permissions);
//                }
//            }
//        }
//
//        if (list.size() > 0) {
//            ActivityCompat.requestPermissions(this, list.toArray(new String[0]), 0);
//            return;
//        }
//
//
//
//        try {
//            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
//            if (cursor != null) {
//                while (cursor.moveToNext()) {
//                    //获取联系人姓名
//                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//                    //                    //获取联系人号码
//                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//                    System.out.println(displayName + "|" + number);
//                }
//            }
//
//            cursor.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//        Log.d("TAG", "onPause");
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        Log.d("TAG", "onStop");
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        Log.d("TAG", "onDestory");
//    }
//
//    @Override
//    protected void onRestart() {
//        super.onRestart();
//        Log.d("TAG", "onRestart");
//    }
//
//    //CTRL + O
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == RESULT_OK) {
//            if (requestCode == 999) {
//                mainTextView.setText(data.getStringExtra("url"));
//            }
//        }
//    }
//        private String[] getPermissions () {
//
//            return new String[]{
//                    Manifest.permission.CAMERA,
//                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                    Manifest.permission.READ_EXTERNAL_STORAGE,
//
//
//                    Manifest.permission.READ_CONTACTS,
//                    Manifest.permission.WRITE_CONTACTS,
//
//
//                    Manifest.permission.READ_SMS,
//                    Manifest.permission.RECEIVE_SMS,
//                    Manifest.permission.SEND_SMS
//
//
//            };
//        }










        //old
//        mainRecyclerView=findViewById(R.id.mainRecyclerView);
//        //发送网络请求，请求接口的数据
//        OkGo.<String>get("https://demo.shopwt.com/api/mobile/index.php?w=goods&t=goods_list&keyword=%E4%BC%91%E9%97%B2&page=10&curpage=1&keyword=%E4%BC%91%E9%97%B2&key=")
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        try {
//                            ArrayList<JSONObject> arrayList = new ArrayList<>();
//                            JSONObject jsonObject = new JSONObject(response.body());
//                            jsonObject = jsonObject.getJSONObject("datas");
//                            JSONArray jsonArray = jsonObject.getJSONArray("goods_list");
//                            for (int i = 0; i < jsonArray.length(); i++) {
//                                arrayList.add(jsonArray.getJSONObject(i));
//                            }
//                            Log.d("TAGS", arrayList.get(0).toString());
//
//                            GoodsListVerAdapter adapter = new GoodsListVerAdapter(arrayList);
//                            //RecyclerView的简单设置
//                            LinearLayoutManager linearLayoutManager= new LinearLayoutManager(MainActivity.this);
//                            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//                            mainRecyclerView.setLayoutManager(linearLayoutManager);
//                            //将适配器与RecyclerView绑定
//                            mainRecyclerView.setAdapter(adapter);
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//        });
//    }
    }
}

